package se.chalmers.tda366.crumble.core.impl;

import java.io.Serializable;
import java.util.Date;

import se.chalmers.tda366.crumble.core.IArticle;

import com.sun.cnpi.rss.elements.Item;


public class Article implements IArticle, Serializable, Comparable<IArticle>{
	
	private String content, headline, source, dateString, description;
	private Date date;

	
	public Article(String h, String s, String c, String d){
		content=c;
		headline=h;
		source=s;
		dateString = d;
	}
	
	public Article(String h, String c){
		content=c;
		headline=h;
		source="http://lolbolln'o'rd.se";
		dateString="TJUGONIONDE MAJ";
	}

	public Article(Item item) {
		try{
			
			if(item.toString().length()>item.getDescription().getText().length())
			{
				content="<html>" + item.toString() + "</html>";
			}else
			{
				content="<html>" + item.getDescription().getText() + "</html>";
			}
			
			headline=item.getTitle().getText();
			source=item.getLink().getText();
			dateString=item.getPubDate().getText();
			description=item.getDescription().getText();
		}catch(NullPointerException e){
			content = "Cant' find article";
			headline = "Can't find article";
			source = "cant find article";
			dateString = "dont know";
			description= "Can't find article";
		}
		
		date=se.chalmers.tda366.crumble.utils.DateParser.parseString(dateString);
		if(date == null)
		{
			date = new Date();
		}

	}

	public String getContent() {
		return content;
	}

	public String getHeadline() {
		return headline;
	}

	public String getSource() {
		return source;
	}

	public String getDateString() {
		return dateString;
	}
	
	public Date getDate() {
		return date;
	}
	
	public String getDescription() {
		return description;
	}

	@Override
	public int compareTo(IArticle o) {
		return o.getDate().compareTo(date);
	}
	
	public boolean equals(Object o){
		if(o==null){
			return false;
		}else if(o instanceof IArticle){
			return date.equals(((IArticle) o).getDate());
		}else{
			return false;
		}
	}
	
	public int hashCode(){
		return date.hashCode();
		
	}
	

}
